const fs = require('fs');
const path = require('path');

// 简单的TypeScript编译模拟
const srcFile = path.join(__dirname, 'src', 'index.ts');
const distFile = path.join(__dirname, 'dist', 'index.js');

// 创建dist目录
if (!fs.existsSync(path.join(__dirname, 'dist'))) {
  fs.mkdirSync(path.join(__dirname, 'dist'));
}

// 读取TypeScript文件并转换为JavaScript
let content = fs.readFileSync(srcFile, 'utf8');

// 简单的TypeScript到JavaScript转换
content = content
  .replace(/import\s+{\s*([^}]+)\s*}\s+from\s+['"]([^'"]+)['"];?/g, (match, imports, module) => {
    if (module.includes('@modelcontextprotocol')) {
      return `const { ${imports} } = require('${module}');`;
    }
    return match;
  })
  .replace(/import\s+([^;]+)\s+from\s+['"]([^'"]+)['"];?/g, (match, importName, module) => {
    if (module.includes('child_process')) {
      return `const { exec } = require('${module}');`;
    }
    if (module.includes('util')) {
      return `const { promisify } = require('${module}');`;
    }
    return match;
  })
  .replace(/async\s+([^(]+)\(/g, 'async $1(')
  .replace(/private\s+/g, '')
  .replace(/public\s+/g, '')
  .replace(/readonly\s+/g, '');

// 写入编译后的文件
fs.writeFileSync(distFile, content);

console.log('Git MCP server built successfully!');

